package com.qianye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianye.entity.*;
import com.qianye.mapper.*;
import com.qianye.service.CustomerService;
import com.qianye.service.LtypeofgoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DeleivertaffMapper deleivertaffMapper;
    @Autowired
    private TransportationMapper transportationMapper;
    @Autowired
    private ExpressInfoMapper expressInfoMapper;
    @Autowired
    private LtypeofgoodsService ltypeofgoodsService;


    @RequestMapping("/login")
    public String login(Customer customer, Model model, HttpSession session) {
        UsernamePasswordToken token = new UsernamePasswordToken(customer.getCusername(), customer.getCpassword());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            session.setAttribute("username", customer.getCusername());
            return "redirect:/toIndex";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名不正确");
            return "forward:/";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码不正确");
            return "forward:/";
        }
    }

    @RequestMapping("/register")
    public String register(Customer customer) {
        int i = customerService.addCustomer(customer);
        return "redirect:/";
    }

    @RequestMapping("/findusername")
    @ResponseBody
    public String findUsername(@RequestParam("username") String username) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cusername", username);

        Customer customer = customerMapper.selectOne(queryWrapper);
        if (customer == null) {
            return "用户名未被占用";
        } else {
            return "用户名已被使用,请重新输入用户名";
        }
    }

    @RequestMapping("/addexpress")
    public String findExpressCate(Model model) {
        List<Transportation> transportationList = transportationMapper.selectList(null);
        List<Category> categoryList = customerService.findAllCate();
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("transportationList", transportationList);
        return "customer/addexpress";
    }

    @RequestMapping("/addexpressinfo")
    @ResponseBody
    public String addExpressInfo(Expressinfo expressinfo, Ltypeofgoods ltypeofgoods, HttpSession session ) {
        String username = (String) session.getAttribute("username");
        expressinfo.setExpressSenderName(username);
        List<Deleivertaff> deleivertaffList = deleivertaffMapper.selectList(null);
        int j = (int) Math.ceil(Math.random() * deleivertaffList.size())-1;
        Deleivertaff deleivertaff = deleivertaffList.get(j);
        expressinfo.setExpressId(deleivertaff.getYid());
        expressinfo.setExpressStatus(1);
        String s = Sid.nextShort();
        ltypeofgoods.setId(s);
        System.out.println(ltypeofgoods);
        expressinfo.setLid(s);
        expressInfoMapper.insert(expressinfo);
        ltypeofgoodsService.insertLtypeofgoods(ltypeofgoods);
        return "添加成功";
    }

    @RequestMapping("/addresseeAll")
    public String addressseeAll(Model model, HttpSession session, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) {
        String username = (String) session.getAttribute("username");
        PageHelper.startPage(pageNo, 5);
        List<Expressinfo> expressinfoList = customerService.addressseeAll(username);
        PageInfo<Expressinfo> pageInfo = new PageInfo<>(expressinfoList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("expressinfoList", expressinfoList);
        return "customer/addressee";
    }

    @RequestMapping("/reciver")
    @ResponseBody
    public String reciverExpress(@RequestParam("kid") Integer kid) throws ParseException {
        System.out.println(kid);
        customerService.reciverExpress(kid);
        return "成功了";
    }

    @RequestMapping("/addrejectexpress")
    @ResponseBody
    public String addRejectExpress(Rejectdetails rejectdetails) {
        customerService.addRejectDetails(rejectdetails);
        return "成功了";
    }

    @RequestMapping("/details")
    public String findCustomerByName(HttpSession session,Model model) {
        String username = (String) session.getAttribute("username");
        Customer customer = customerService.findCustomerByName(username);
        model.addAttribute("customer",customer);
        return "customer/details";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String updateCustomer(Customer customer) {
        customerMapper.updateById(customer);
        return "成功了";
    }

    @RequestMapping("/ownersent")
    public String ownerSent(HttpSession session,Model model,@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo) {
        String  username = (String) session.getAttribute("username");
        List<Expressinfo> expressinfoList = customerService.findOwnerSent(username);
        PageHelper.startPage(pageNo,5);
        PageInfo<Expressinfo> pageInfo = new PageInfo<>(expressinfoList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("expressinfoList",expressinfoList);
        return "customer/sent";
    }

    @RequestMapping("/receive")
    public String findMyreceive(HttpSession session,Model model,@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo) {
        String  username = (String) session.getAttribute("username");
        List<Expressinfo> expressinfoList = customerService.findMyReceive(username);
        PageInfo<Expressinfo> pageInfo = new PageInfo<>(expressinfoList);
        model.addAttribute("expressinfoList",expressinfoList);
        model.addAttribute("pageInfo",pageInfo);
        return "customer/receive";
    }
}
